package com.vicular.product;

import com.vicular.PreferencesActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;

public class ProductDbHelper extends SQLiteOpenHelper {

	static int DB_VERSION = 1;
	private static final String DB_NAME = "product.sqlite";
	static final String TABLE_NAME = "product";
	static final String ID_COL = "id";
	static final String BAECODE_COL = "barcode";
	static final String PNAME_COL = "pname";
	static final String ENAME_COL = "ename";
	static final String EADDR_COL = "eaddr";
	static final String EZCODE_COL = "ezcode";
	static final int colNum = 5;
	private Context context;

	ProductDbHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID_COL
				+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + BAECODE_COL
				+ " TEXT, " + PNAME_COL + " TEXT, " + ENAME_COL + " TEXT, "
				+ EADDR_COL + " TEXT, " + EZCODE_COL + " INTEGER);");
		SharedPreferences.Editor editor = PreferenceManager
				.getDefaultSharedPreferences(context).edit();
		editor.putInt(PreferencesActivity.KEY_SQLITE_VERSION, DB_VERSION);
		editor.commit();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

}
